import { _decorator, Color, Component, director, geometry, GeometryRenderer, Node, renderer } from 'cc';
import { CarlosEngine } from './libs/carlos-engine';
import { CarlosI } from './libs/carlos-i';
import { EDITOR } from 'cc/env';
const { ccclass, property, executeInEditMode } = _decorator;
export const GeometryColor: Color = new Color(0, 255, 0);


@ccclass('Item')
@executeInEditMode
export class item extends Component {
    private _node: CarlosI.Node = null;
    private _collider: CarlosI.ColliderBase = null;

    protected _geometryRenderer: GeometryRenderer = null;

    private _aabb: geometry.AABB = new geometry.AABB();
    start() {
        this.scheduleOnce(this.init, 0.2);

    }
    private init(): void {
        this._node = CarlosEngine.instance.addNode(this.node.uuid);
        this._collider = this._node.addBoxCollider();
        // console.log(this._collider)
        this._node?.setPosition(this.node.worldPosition.x, this.node.worldPosition.y, this.node.worldPosition.z)
        this._node?.setRotation(this.node.worldRotation.x, this.node.worldRotation.y, this.node.worldRotation.z, this.node.worldRotation.w)

        this.node.on(Node.EventType.TRANSFORM_CHANGED, () => {
            this._node?.setPosition(this.node.worldPosition.x, this.node.worldPosition.y, this.node.worldPosition.z)
            this._node?.setRotation(this.node.worldRotation.x, this.node.worldRotation.y, this.node.worldRotation.z, this.node.worldRotation.w)
        }, this)

        if (this.node.name == "Cube") {
            this._collider.setColliderCallback((uuid: string) => {
                console.log("碰撞检测：uuid", uuid);
            });
        }
    }

    update(deltaTime: number) {
        if (!this._geometryRenderer) {
            this._geometryRenderer = this.getGeometryRenderer();
        }
        if (!this._geometryRenderer) return;
        this.debugDrawGeometry();
    }
    protected debugDrawGeometry(): void {
        if (!this._collider) return;
        let aabb: CarlosI.AABB = this._collider.getAABB();
        this._aabb.center.set(aabb.getCenter().getX(), aabb.getCenter().getY(), aabb.getCenter().getZ());
        this._aabb.halfExtents.set(aabb.getHalfExtents().getX(), aabb.getHalfExtents().getY(), aabb.getHalfExtents().getZ());
        this._geometryRenderer.addBoundingBox(this._aabb,GeometryColor,true,false);
    }

    protected getGeometryRenderer(): GeometryRenderer {
        let camera = this.getCamera();
        camera.initGeometryRenderer();
        this._geometryRenderer = camera.geometryRenderer;
        return this._geometryRenderer;
    }
    private getCamera(): renderer.scene.Camera {
        let camera: renderer.scene.Camera = null;
        director.root.scenes.forEach(s => {
            s.cameras.forEach(c => {
                if (EDITOR) {
                    if (c.name === 'Editor UIGizmoCamera') {
                        camera = c;
                        return;
                    }
                } else {
                    if (c.name === 'Main Camera') {
                        camera = c;
                        return;
                    }
                }
            })
        });
        return camera;
    }
}

